package br.nextfm.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.nextfm.dao.CategoriaDao;
import br.nextfm.model.Categoria;

public class CadastraCategoriaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String nome = request.getParameter("nome");
		
		Categoria categoria = new Categoria(nome);
		
		CategoriaDao dao = new CategoriaDao();
		
		if (!dao.insert(categoria)) {
			
			request.setAttribute("erroCadastraCategoria", true);
			
		}
		
		request.setAttribute("categorias", dao.getAll());
		request.getRequestDispatcher("categoria.jsp").forward(request, response);
		
	}

}
